import {
    getCurrentUserPagePermissionsApi,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsSuccess
} from 'Shared/actions/currentUserContext';
import {PAGE_CODE} from './constants';
import * as api from './api';
/*==============================初始化API================================= */
export const GET_INIT_BEGIN = 'GET_INIT_BEGIN';
export const GET_INIT_SUCCESS = 'GET_INIT_SUCCESS';
export const GET_INIT_FAIL = 'GET_INIT_FAIL';
export const getInitDataBegin = () => ({
    type: GET_INIT_BEGIN
});
export const getInitDataSuccess = data => ({
    type: GET_INIT_SUCCESS,
    data,
});
export const getInitDataFail = () => ({
    type: GET_INIT_FAIL,
});
export const getInitData = (code = PAGE_CODE) => dispatch => getCurrentUserPagePermissionsApi(code).then(res => {
    if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
    else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
    return res.isOk;
});

/*==============================查询列表================================= */

export const GET_LIST_BEGIN = 'GET_LIST_BEGIN';
export const GET_LIST_SUCCESS = 'GET_LIST_SUCCESS';
export const GET_LIST_FAIL = 'GET_LIST_FAIL';
export const getListBegin = () => ({
    type: GET_LIST_BEGIN
});
export const getListSuccess = (data, condition) => ({
    type: GET_LIST_SUCCESS,
    data,
    condition
});
export const getListFail = () => ({
    type: GET_LIST_FAIL,
}); //获取列表

export const getList = condition => dispatch => {
    dispatch(getListBegin());
    return api.getList(condition).then(res => {
        if(res.ok)
            dispatch(getListSuccess(res.data, condition));
        else
            dispatch(getListFail());
        return res.ok;
    });
}; //按钮搜索

export const querySearch = () => (dispatch, getState) => {
    const condition = getState()
        .getIn(['page', 'appState', 'queryCondition'])
        .toJS();
    dispatch(getList(condition));
}; //分页排序搜索

export const tableSearch = page => (dispatch, getState) => {
    const query = getState()
        .getIn(['page', 'appState', 'queryBySort'])
        .toJS();
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

//重置查询条件

export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});

/*==============================编辑服务满意度调查================================ */
export const onClickUpdateInfo = (data, id) => () => {
    if(id)
        return api.updateInfo(id, {
            questionContent: data.questionContent,
            code: data.code,
            enQuestionContent: data.enQuestionContent,
            esQuestionContent: data.esQuestionContent,
            ptQuestionContent: data.ptQuestionContent,
            ruQuestionContent: data.ruQuestionContent,
            ukQuestionContent: data.ukQuestionContent,
            arQuestionContent: data.arQuestionContent,
            faQuestionContent: data.faQuestionContent,
            description: data.description,
            enDescription: data.enDescription,
            esDescription: data.esDescription,
            ukDescription: data.ukDescription,
            ptDescription: data.ptDescription,
            ruDescription: data.ruDescription,
            arDescription: data.arDescription,
            faDescription: data.faDescription,
            isCheck: data.isCheck,
            rowVersion: data.rowVersion,
            details: data.details,
        }).then(res => res.ok);
    
        
    return api.addInfo({
        questionContent: data.questionContent,
        code: data.code,
        enQuestionContent: data.enQuestionContent,
        esQuestionContent: data.esQuestionContent,
        ptQuestionContent: data.ptQuestionContent,
        ruQuestionContent: data.ruQuestionContent,
        ukQuestionContent: data.ukQuestionContent,
        arQuestionContent: data.arQuestionContent,
        faQuestionContent: data.faQuestionContent,
        description: data.description,
        enDescription: data.enDescription,
        ukDescription: data.ukDescription,
        esDescription: data.esDescription,
        ptDescription: data.ptDescription,
        ruDescription: data.ruDescription,
        arDescription: data.arDescription,
        faDescription: data.faDescription,
        isCheck: data.isCheck,
        details: data.details,
    }).then(res => res.ok);
};
/*==============================作废================================ */
export const onClickAbandonInfo = id => (dispatch, getState) => {
    const data = getState()
        .getIn(['page', 'domainData', 'list', 'data'])
        .toJS();
    const {rowVersion} = data.find(item => item.id === id);
    return api.abandonInfo(id, {
        rowVersion
    }).then(res => res.ok);
};
/*==============================生效================================ */
export const onClickEffectiveInfo = id => (dispatch, getState) => {
    const data = getState()
        .getIn(['page', 'domainData', 'list', 'data'])
        .toJS();
    const {rowVersion} = data.find(item => item.id === id);
    return api.effectiveInfo(id, {
        rowVersion
    }).then(res => res.ok);
};
